package com.jed.xxh.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jed.xxh.R;
import com.jed.xxh.entity.ChatListData;

import java.util.List;

/**
 * 项目名：stewardAPP<br/>
 * 包名：  com.jed.xxh.adapter<br/>
 * 文件名：ChatListAdapter<br/>
 * 作者：徐晓辉<br/>
 * 创建日期：2018/11/24 14:02<br/>
 * 描述：对话adapter<br/>
 */
public class ChatListAdapter extends MyBaseAdapter {

    //左边的type
    public static final int VALUE_LEFT_TEXT = 1;
    //右边的type
    public static final int VALUE_RIGHT_TEXT = 2;

    private List<ChatListData> mList;

    public ChatListAdapter(List<ChatListData> mList) {
        super(mList);
        this.mList = mList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderLeftText viewHolderLeftText = null;
        ViewHolderRightText viewHolderRightText = null;
        //获取当前要显示的type 根据这个type来区分数据的加载
        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case VALUE_LEFT_TEXT:
                    viewHolderLeftText = new ViewHolderLeftText();
                    convertView = inflater.inflate(R.layout.left_item, null);
                    viewHolderLeftText.tv_left_text = convertView.findViewById(R.id.tv_left_text);
                    convertView.setTag(viewHolderLeftText);
                    break;
                case VALUE_RIGHT_TEXT:
                    viewHolderRightText = new ViewHolderRightText();
                    convertView = inflater.inflate(R.layout.right_item, null);
                    viewHolderRightText.tv_right_text = convertView.findViewById(R.id.tv_right_text);
                    convertView.setTag(viewHolderRightText);
                    break;
            }
        } else {
            switch (type) {
                case VALUE_LEFT_TEXT:
                    viewHolderLeftText = (ViewHolderLeftText) convertView.getTag();
                    break;
                case VALUE_RIGHT_TEXT:
                    viewHolderRightText = (ViewHolderRightText) convertView.getTag();
                    break;
            }
        }

        //赋值
        ChatListData data = mList.get(position);
        switch (type) {
            case VALUE_LEFT_TEXT:
                viewHolderLeftText.tv_left_text.setText(data.getText());
                break;
            case VALUE_RIGHT_TEXT:
                viewHolderRightText.tv_right_text.setText(data.getText());
                break;
        }
        return convertView;
    }

    //根据数据源的positiion来返回要显示的item
    @Override
    public int getItemViewType(int position) {
        ChatListData data = mList.get(position);
        return data.getType();
    }

    //返回所有的layout数据
    @Override
    public int getViewTypeCount() {
        return 3; //mlisy.size + 1
    }

    //左边的文本
    private class ViewHolderLeftText {
        private TextView tv_left_text;
    }

    //右边的文本
    private class ViewHolderRightText {
        private TextView tv_right_text;
    }
}
